package com.iwords.android;

import org.apache.http.client.methods.HttpPost;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class FeedBack extends Activity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feed_back);
		try{
			getLatest();
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
	}

	private void getLatest(){
		
	}
	
	private void vibrate(){
		Vibrator mVibrator = (Vibrator)getApplication().getSystemService(android.app.Service.VIBRATOR_SERVICE);
		mVibrator.vibrate(new long[]{50,50},-1);
	}
    public void commit(View view) {
    	this.vibrate();
    	EditText comments = (EditText)findViewById(R.id.feed_back_comments);
    	if(comments.getText().length()<1) return;
    	String paramString = "&userid="+((IWordsApp) getApplicationContext()).getUser().getUserid()+"&comments="+CodeUtils.encode(comments.getText().toString());
    	//paramString += "&comments0="+comments.getText().toString();
    	//paramString += "&comments2="+CodeUtils.encode(CodeUtils.encode(comments.getText().toString()));
    	
    	if(save(paramString)){
    		Intent intent = new Intent(this, FeedBackList.class);
    		this.startActivity(intent);
    	}
    	this.finish();
    }
	private boolean save(String paramString){
		String href = this.getResources().getString(R.string.feed_back_commit);
		//System.out.println("FeedBack.save()! href="+href);
		try {
			
			href = href + paramString;
			
			//System.out.println("FeedBack.save()! href="+href);
			HttpPost request = new HttpPost(href);
			JSONObject jSONObject = ServerAccessor.access(this, request);
			boolean success = jSONObject.getBoolean("isSuccess");
			if(success){
				displayMessage("保存成功。");
			}else{
				displayMessage(CodeUtils.decode(jSONObject.getString("message")));
			}
			return success;
		}catch(Exception e){
			//e.printStackTrace();
			displayMessage(e.getMessage());
		}
		return false;
    }
	private void displayMessage(String message){
		Toast t1 = Toast.makeText(getApplication(), message, Toast.LENGTH_SHORT);
		t1.show();
	}
	
	public void onResume() {
		super.onResume(); 
		com.mobclick.android.MobclickAgent.onResume(this);
	}
	
	public void onPause() {
		super.onPause(); 
		com.mobclick.android.MobclickAgent.onPause(this);
	}
}
